package com.showsu.editor.service.impl;

import com.showsu.editor.service.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashSet;


/**
 * @ClassName: MailServiceImpl
 * @Description:
 * @Author: shengyi
 * @Date: 2022/7/27 11:39
 */
@Service
@Slf4j
public class MailServiceImpl implements IMailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String emailUserName;

    @Value("${spring.mail.alarmEmail}")
    private String alarmEmail;

    @Override
    public void sendMail(String alarmEmail, String title, String content,String personal) {
        new HashSet<>(Arrays.asList(alarmEmail.split(","))).forEach(email -> {
            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setFrom(emailUserName, personal);
                helper.setTo(email);
                helper.setSubject(title);
                helper.setText(content, true);
                mailSender.send(mimeMessage);
            } catch (UnsupportedEncodingException | MessagingException e) {
                log.error("邮件发送-alarmEmail:{}-title:{},异常:{}",alarmEmail,title, e);
//                e.printStackTrace();
            }
        });
    }

    @Override
    public void sendMail(String alarmEmail, String title, String content) {
        new HashSet<>(Arrays.asList(alarmEmail.split(","))).forEach(email -> {
            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setFrom(emailUserName, "视频去水印");
                helper.setTo(email);
                helper.setSubject(title);
                helper.setText(content, true);
                mailSender.send(mimeMessage);
            } catch (UnsupportedEncodingException | MessagingException e) {
                log.error("邮件发送-alarmEmail:{}-title:{},异常:{}",alarmEmail,title, e);
//                e.printStackTrace();
            }
        });
    }

    /**
     * 接收着在配置文件里面配置
     * 发送邮件
     * @param title
     * @param content
     */
    @Override
    public void sendMail(String title, String content) {
        new HashSet<>(Arrays.asList(alarmEmail.split(","))).forEach(email -> {
            try {
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setFrom(emailUserName, "视频去水印");
                helper.setTo(email);
                helper.setSubject(title);
                helper.setText(content, true);
                mailSender.send(mimeMessage);
            } catch (UnsupportedEncodingException | MessagingException e) {
                log.error("邮件发送-title:{},异常:{}",title, e);
//                e.printStackTrace();
            }
        });
    }
}
